package org.example.synchronizeddemo;

/**
 * 对象锁示例1 代码块形式
 */
public class synchronizedObjectCodeBlock2 implements Runnable {

    private static final synchronizedObjectCodeBlock2 syn = new synchronizedObjectCodeBlock2();

    final Object lock1 = new Object();

    final Object lock2 = new Object();

    @Override
    public void run() {
        synchronized (lock1) {
            System.out.println("第一把，我是对象锁代码块形式我叫：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束");
        }

        synchronized (lock2) {
            System.out.println("第二把，我是对象锁代码块形式我叫：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束");
        }



    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(syn);
        Thread thread2 = new Thread(syn);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {

        }

        System.out.println("结束");
    }

}
